<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: AddAd.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\home\model\page\shop;

use app\api\model\dao\Ad;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;

class AddAd extends PageBase
{

    private $objAd;

    public function __construct()
    {
        parent::__construct();
        $this->objAd = new Ad();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'ad_name' => 'require',
            'pid' => 'require|number',
            'ad_link' => 'require',
            'ad_code' => 'require',
            'ad_type' => 'require|number',
            'orderby' => 'require|number',

            //'media_type'     => 'require',
            //'start_time'     => 'require',
            //'end_time'     => 'require',
            //'link_man'     => 'require',
            //'link_email'     => 'require',
            //'link_phone'     => 'require',
            //'ad_value'     => 'require',
        ];
        $msg = [
            'ad_name.require' => '广告名称必填',
            'pid.require' => '广告名称必填',
            'ad_link.require' => '广告链接必填',
            'ad_code.require' => '广告代码必填',
            'ad_type.require' => '广告链接类型必填',
            'orderby.require' => '广告排序必填',
            //'media_type.require'      => '广告类型必填',
            //'start_time.require'      => '广告开始时间必填',
            //'end_time.require'      => '广告结束时间必填',
            //'link_man.require'      => '联系人必填',
            //'link_email.require'      => '联系邮件必填',
            //'link_phone.require'      => '联系电话必填',
            //'ad_value.require'      => '广告链接值必填',

        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        //var_dump($param);
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        // TODO:业务逻辑
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $ret = $this->objAd->addAd($param);
        if ($ret === false) {
            return $this->getRes(Error::ERR_SYS);
        }

        $this->data = array(
            'ad_id' => $ret,
        );

        return $this->getRes();
    }
}
